package com.quanquan.store.controller;

import DTO.PageDTO;
import com.quanquan.store.dto.ShopMallDTO;
import com.quanquan.store.request.ShopMallRequest;
import com.quanquan.store.service.ShopMallService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import web.response.ResultBody;

/**
 * @ClassName:ShopMallController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/8/13 10:45
 * @Version 1.0
 */
@RestController
@RequestMapping("/shop_mall")
public class ShopMallController {

    public static final String SUCCESS = "创建成功";
    public static final String UPDATE_SUCCESS = "更新成功";
    public static final String DELETE_SUCCESS = "删除成功";
    private final ShopMallService shopMallService;

    @Autowired
    public ShopMallController(ShopMallService shopMallService) {
        this.shopMallService = shopMallService;
    }

    /**
     * 管理端创建商城商品
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ResultBody createShopingMall(@RequestBody ShopMallRequest requestBody) {
        String msg = shopMallService.saveShopMall(requestBody);
        if(msg.equals(SUCCESS)){
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), msg);
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), msg);
    }

    /**
     * 管理端更新商城商品
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultBody updateShopingMall(@RequestBody ShopMallRequest requestBody) {
        String msg = shopMallService.updateShopMall(requestBody);
        if(msg.equals(UPDATE_SUCCESS)){
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), msg);
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), msg);
    }

    /**
     * 多条件查询
     *
     * @param request
     * @return
     */
    @PostMapping("query_select")
    public ResultBody queryByCondition(@RequestBody ShopMallRequest request) {
        PageDTO<ShopMallDTO> couponPageDTO;
        couponPageDTO = shopMallService.queryByCondition(request);
        if (null != couponPageDTO && couponPageDTO.getList().size() > 0) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), couponPageDTO);
        }
        return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());

    }

    /**
     * 根据id查询商城商品
     *
     * @param request
     * @return
     */
    @PostMapping("query_by_id")
    public ResultBody queryById(@RequestBody ShopMallRequest request) {
        ShopMallDTO shopMallDTO = shopMallService.findById(request.getId());
        if (null != shopMallDTO) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), shopMallDTO);
        }
        return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());

    }

    /**
     * 删除商城对应商品
     *
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public ResultBody deleteShopMall(@RequestBody ShopMallRequest request) {
        String msg = shopMallService.deleteShopMall(request);
        if (msg.equals(DELETE_SUCCESS)) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), msg);
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), msg);
    }


    /**
     * 管理端更新商城商品
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/update_status")
    @ResponseBody
    public ResultBody updateStatus(@RequestBody ShopMallRequest requestBody) {
        boolean msg = shopMallService.updateStatus(requestBody);
        if(msg){
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS");
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
    }

}
